﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Timers;
using System.Windows.Forms;
using AgentTest;
using DXWinFormDataAnalysis.DataModel;
using DevExpress.XtraEditors;
using GeneralDFS.Client;
using GeneralDFS.Common.Network;
using GeneralDFS.DataEntity.Config;
using GeneralDFS.DataEntity.Statistics;
using GeneralDFS.Interface.Agent;
using NIF.MobileActors.Core;
using NIF.MobileActors.Hosts;

namespace DXWinFormDataAnalysis
{
    public partial class DNCounter : DevExpress.XtraEditors.XtraForm
    {
        private readonly Client _client;
        private DFSConfig _config;
        private WaitForm1 _wform;
        private DataNodeActor _act;
        private readonly List<DataNodeCounter> dataNodeCounters;

        public DNCounter()
        {
            InitializeComponent();
            _client = new Client();
            _config = _client.GetConfig();
            dataNodeCounters = new List<DataNodeCounter>();
        }

        private void bt_Collect_Click(object sender, EventArgs e)
        {
            _wform = new WaitForm1();
            _wform.StartPosition = FormStartPosition.CenterScreen;
            _wform.Show();
            ActorProxy<IEntry> localActor = RemotingHost.LocalFacade.CreateLocalActor<IEntry>(typeof(DataNodeActor));
            _act = localActor.GetActor() as DataNodeActor;
            var client = new Client();
            var hosts = new Queue<IPEndPoint>();
            foreach (DataNodeConfig dataNodeConfig in client.GetConfig().DataNodeConfigs)
            {
                var endPoint = new IPEndPoint(IPAddress.Parse(dataNodeConfig.Ip), dataNodeConfig.AgentRpcPort);
                hosts.Enqueue(endPoint);
            }

            System.Diagnostics.Debug.Assert(_act != null);
            _act.Hosts = hosts;
            _act.LocalHost = new IPEndPoint(IPAddress.Parse(IPHelper.GetLocalIP("network")), 8820);
            try
            {

                localActor.Post(h => h.Excute());
                var timer = new System.Timers.Timer(10);
                timer.Elapsed += timer_Elapsed;
                timer.Start();
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var timer = (System.Timers.Timer)sender;
            if (_act.IsLastHost)
            {
                try
                {
                    timer.Stop();

                    this.Invoke(new MethodInvoker(delegate
                    {
                        gridControl1.BeginUpdate();
                        gridControl1.DataSource = dataNodeCounters;
                        gridControl1.EndUpdate();
                        if (_wform != null && _wform.WindowState == FormWindowState.Normal)
                            _wform.Close();
                    }));
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}